<?php

namespace App\Mail;

use App\Models\EStatement\EStatement;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Models\Member\Rider;
use Barryvdh\DomPDF\Facade as PDF;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;

class EStatementMail extends Mailable
{
    use Queueable, SerializesModels;

    protected $rider;
    protected $eStatement;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Rider $rider, EStatement $eStatement)
    {
        $this->rider = $rider;
        $this->eStatement = $eStatement;
        $this->subject = 'Statement Report ' . Carbon::now()->format('Y M');
        $this->mailer = 'Bukit Timah Saddle Club'; 
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $e_statement = $this->eStatement;
        $pdf = PDF::loadView('pdf.e-statement',compact('e_statement'));
		$pdf->setPaper('a4');
        
        $fileName = time() . '_' . mt_rand(0, 99999) . '.pdf';
        Storage::disk('public')->put("temp/pdf/$fileName", $pdf->output());

        return $this->view('email.e_statement')
                    ->attach(public_path('storage/temp/pdf') . '/' . $fileName, [
                        'as' => 'BTSC_E_Statement_' . Carbon::now()->format('Y_m') . '_' .$this->rider->name,
                        'mime' => 'application/pdf',
                    ])
                    ->with([
                        'rider' => $this->rider,
                    ]);
    }
}
